package com.koicarp.university.graduate.service.service.wx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.LoginSessionWxDao;
import com.koicarp.university.graduate.service.entity.LoginSessionWx;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 【微信：微信登录表】(LoginSessionWx)表服务实现类
 *
 * @author liutao
 * @since 2020-10-24 11:15:55
 */
@Slf4j
@Service("loginSessionWxService")
@RequiredArgsConstructor
public class LoginSessionWxServiceImpl extends ServiceImpl<LoginSessionWxDao,LoginSessionWx> implements ILoginSessionWxService {

    private final LoginSessionWxDao loginSessionWxDao;

    @Override
    public UniversityResponse login(String openId, String token) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);

        log.info("openId:                        "+openId);
        LambdaQueryWrapper<LoginSessionWx> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LoginSessionWx::getOpenId,openId);
        List<LoginSessionWx> list = loginSessionWxDao.selectList(wrapper);

        if (list.size() > 0){
            list.get(0).setToken(token);
            int update = loginSessionWxDao.update(list.get(0));
            if (update < 0){
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
                return response;
            }
            response.data(list.get(0));
        }else{
            LoginSessionWx loginWx = new LoginSessionWx();
            loginWx.setOpenId(openId);
            loginWx.setToken(token);
            loginWx.setGraduateId(null);
            int insert = loginSessionWxDao.insert(loginWx);
            //（毕业生）信息未绑定
            response.message("账号未与信息绑定");
            if (insert < 0){
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
                return response;
            }
            response.data(loginWx);
        }
        return response;
    }

    @Override
    public UniversityResponse checkToken(String token) {
        LambdaQueryWrapper<LoginSessionWx> wrapper = new LambdaQueryWrapper<>();
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);

        wrapper.eq(LoginSessionWx::getToken,token);
        List<LoginSessionWx> list = loginSessionWxDao.selectList(wrapper);
        if (list.size()>0) { // 数据库查到此token，表示token有效并返回userId
            response.data(list.get(0));
        } else { // 数据库中没查到此token，表示token过期或者无此记录
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LoginSessionWx queryById(Integer id) {
        return this.loginSessionWxDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LoginSessionWx> queryAllByLimit(int offset, int limit) {
        return this.loginSessionWxDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param loginSessionWx 实例对象
     * @return 实例对象
     */
    @Override
    public LoginSessionWx insert(LoginSessionWx loginSessionWx) {
        this.loginSessionWxDao.insert(loginSessionWx);
        return loginSessionWx;
    }

    /**
     * 修改数据
     *
     * @param loginSessionWx 实例对象
     * @return 实例对象
     */
    @Override
    public LoginSessionWx update(LoginSessionWx loginSessionWx) {
        this.loginSessionWxDao.update(loginSessionWx);
        return this.queryById(loginSessionWx.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.loginSessionWxDao.deleteById(id) > 0;
    }
}